package app.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.annvcit.imagewrite.CardInfo;
import com.qualcomm.vuforia.CloudRecognition.utils.PostNewTarget;

/**
 * Servlet implementation class CreateCard
 */
@WebServlet(name = "createcard", urlPatterns = { "/createcard" })
public class CreateCard extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProc(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProc(request, response);
	}

	protected void doProc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		String modelName = (String)request.getParameter("options");
		String imageLocation = (String)session.getAttribute("imageLocation");
		CardInfo cardInfo = (CardInfo)session.getAttribute("cardInfo");
		cardInfo.setModelName(modelName);
		
		final PostNewTarget p = new PostNewTarget();
		p.setImageLocation(imageLocation);
		p.setMetadata(cardInfo.toString());
		
		session.removeAttribute("cardInfo");
		session.removeAttribute("imageLocation");
		
		new Thread(){
			public void run() {
				p.postTargetThenPollStatus();
			};
		}.start();
		
		request.getRequestDispatcher("/success.jsp").forward(request, response);
	}
	

}
